package com.wfit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfit.context.UserContext;
import com.wfit.dto.CateDTO;
import com.wfit.dto.CatePageVO;
import com.wfit.dto.PageDTO;
import com.wfit.enumeration.CateStatusEnum;
import com.wfit.mapper.CateMapper;
import com.wfit.mapper.UsersMapper;
import com.wfit.po.Cate;
import com.wfit.po.Commodity;
import com.wfit.po.Users;
import com.wfit.result.Result;
import com.wfit.service.CateService;
import com.wfit.service.UploadService;
import com.wfit.vo.CartVO;
import com.wfit.vo.CateVO;
import com.wfit.vo.CommodityBusinessVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CateServiceImpl extends ServiceImpl<CateMapper, Cate> implements CateService {

    @Autowired
    private CateMapper cateMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UploadService uploadService;
    @Override
    public Result search(CatePageVO dto) {
        Page<Cate> page = dto.toMpPageDefaultSortByCreateTimeDesc();
        LambdaQueryWrapper<Cate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dto.getName() != null,Cate::getName, dto.getName());
        queryWrapper.eq(dto.getStatus() != null,Cate::getStatus, dto.getStatus());

        page = page(page,queryWrapper);
        PageDTO<CateVO> pageVO = PageDTO.of(page,  (cate) -> {
                Long creator = cate.getCreator();
                Users users = usersMapper.selectById(creator);
                String nickName = users.getNickName();
                if (users == null) {
                    nickName = "无";
                }
                CateVO cateVO = new CateVO();
                BeanUtils.copyProperties(cate,cateVO);
                cateVO.setCreatorName(nickName);
                return cateVO;
            });
        List<CateVO> list = pageVO.getList();
        ArrayList<CateVO> cartVOS = new ArrayList<>();
        for (CateVO cateVO : list) {
            if (cateVO.getPId() == null) {
                cartVOS.add(cateVO);
            }
        }
        for (CateVO cateVO : cartVOS) {
            ArrayList<CateVO> cateVOS = new ArrayList<>();
            for (CateVO vo : list) {
                if (vo.getPId() != null && vo.getPId().equals(cateVO.getId())) {
                    cateVOS.add(vo);
                }
            }
            cateVO.setChildren(cateVOS);
        }
        pageVO.setList(cartVOS);
        return Result.success(pageVO);
    }

    @Override
    public Result listCate() {

        List<Cate> list = list(Wrappers.<Cate>lambdaQuery().eq(Cate::getStatus, CateStatusEnum.ON.getStatus()));
        list = list.stream().filter(cate -> cate.getPId() == null).collect(Collectors.toList());
        return Result.success(list);
    }

    @Override
    public Result insertCate(CateDTO dto) throws IOException {
        Cate cate = new Cate();
        BeanUtils.copyProperties(dto,cate,"image");
        String upload = uploadService.upload(dto.getImage());
        cate.setImage(upload);
        cate.setCreateTime(LocalDateTime.now());
        cate.setStatus(CateStatusEnum.ON.getStatus().shortValue());
        cate.setCreator(UserContext.getUser());
        save(cate);
        return Result.success();
    }

    @Override
    public Result updateCate(CateDTO dto) throws IOException {
        Cate cate = new Cate();
        BeanUtils.copyProperties(dto,cate,"image");
        String upload = uploadService.upload(dto.getImage());
        cate.setImage(upload);
        updateById(cate);
        return Result.success();
    }

    @Override
    public Result saveCh(CateDTO dto,Long pId) throws IOException {
        String upload = uploadService.upload(dto.getImage());
        Cate cate = new Cate();
        cate.setName(dto.getName());
        cate.setImage(upload);
        cate.setCreateTime(LocalDateTime.now());
        cate.setStatus(CateStatusEnum.ON.getStatus().shortValue());
        cate.setCreator(UserContext.getUser());
        cate.setPId(pId);
        save(cate);
        return Result.success();
    }

    @Override
    public Result getChilderen(Integer id) {
        if (id == null) {
            return Result.error("参数错误");
        }
        List<Cate> list = list(Wrappers.<Cate>lambdaQuery().eq(Cate::getPId, id));
        return Result.success(list);
    }
}
